var 
    appconfig = require('../utils/appconfig'),
    Q = require('q'),
    mongodb = require('mongodb');


var _cfg = appconfig.getCfg();
var _mongo_url = 'mongodb://'+
  _cfg.mongodb.host+':'+_cfg.mongodb.port+'/'+_cfg.mongodb.db;



var _delete = function (collection,query,callback){
  mongodb.MongoClient.connect(_mongo_url, function(err, db) {
      if(!!err){
        throw err;
      }
      var _c = db.collection(collection);
      _c.remove(query, {w:1}, function(err, rows){
        callback(rows);
        db.close();
      });   
  });  
};


var _insert = function (collection,docs,callback){
  mongodb.MongoClient.connect(_mongo_url, function(err, db) {
      if(!!err){
        throw err;
      }
      var _c = db.collection(collection);
      _c.insert(docs,function(err, _docs){
        callback(_docs);
        db.close();
      });   
  }); 
};


var _update = function(collection,query,doc,callback){
  mongodb.MongoClient.connect(_mongo_url, function(err, db) {
    var _c = db.collection(collection);
    _c.findAndModify(query, [], doc,{},function(err, _docs){
      callback(_docs);
      db.close();
    });
  });
};

var _get = function (collection, query , callback){
  mongodb.MongoClient.connect(_mongo_url, function(err, db) {
      if(!!err){
        throw err;
      }
      var _c = db.collection(collection);
      _c.findOne(query,function (err, doc) {
        callback(doc);
        db.close();
      }); 
  });  
};

// orderField: field:asc,field:desc
var _find = function (collection,query,orderField,callback){
  mongodb.MongoClient.connect(_mongo_url, function(err, db) {
      if(!!err){
        throw err;
      }
      var _c = db.collection(collection);


      var options = {};
      if (!!orderField){
        options.sort = [];
        var _arr = orderField.split(',');
        for(var i=0,l=_arr.length;i<l;i++){
          options.sort.push(
            [ 
              _arr[i].split(':')[0],
              _arr[i].split(':')[1]
            ]);
        }
      }

      _c.find(query,options).toArray(function (err, docs) {
        callback(docs);
        db.close();
      });        
  });  
};


// following is promosed 
exports.update = function(collection,query,doc){
  var deferred = Q.defer();  
  _update(collection,query,doc,function(_doc){
    deferred.resolve(_doc);
  });
  return deferred.promise;
};


exports.insert = function(collection,docs){
  var deferred = Q.defer();  
  _insert(collection, docs, function(_docs){
      deferred.resolve(_docs);
  });
  return deferred.promise;  
};

exports.get = function (collection,query){
  var deferred = Q.defer();  
  _get(collection, query, function(doc){
      deferred.resolve(doc);
  });
  return deferred.promise;  
};

exports.find = function(collection,query,orderField){
  var deferred = Q.defer(); 
  _find(collection,query,orderField,function(docs){
      deferred.resolve(docs);
  });
  return deferred.promise;
};


exports.delete = function(collection,query){
  var deferred = Q.defer();
  _delete(collection,query,function (rows){
      deferred.resolve(rows);
  });
  return deferred.promise;
};

